<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['money:mtpMstr:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['money:mtpMstr:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-print"
          size="mini"
          @click="handlePrint"
          v-hasPermi="['money:mtpMstr:export']"
          >打印</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            v-hasPermi="['marketing:som:import']"
          >
            批量导入
          </el-button>
        </el-col>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['money:mtpMstr:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['money:mtpMstr:export']"
        >
          导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      border
      stripe
      v-loading="loading"
      @scrollBottom="load"
      height="calc(100% - 50px)"
      :data="mtpMstrList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="资产编号"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpOrder"
      />
      <el-table-column
        label="设备类别"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpEquipmentType"
      />
      <el-table-column
        label="资产名称"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpName"
      />
      <el-table-column
        label="规格型号"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpModel"
      />
      <el-table-column
        label="设备厂商"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpEquipmentFirm"
      />
      <el-table-column
        label="购入日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpBuyDate"
      />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpUnit"
      />
      <el-table-column
        label="数量"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpNum"
      />
      <el-table-column
        label="使用部门"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpDept"
      />
      <el-table-column
        label="责任人"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpDutyName"
      />
      <el-table-column
        label="存放地点"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpLocation"
      />
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        align="center"
        prop="mtpDesc"
      />
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['money:mtpMstr:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['money:mtpMstr:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="12">
          <el-form-item label="资产编号" prop="mtpOrder">
            <el-input
              v-model="queryParams.mtpOrder"
              placeholder="请输入资产编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资产名称" prop="mtpName">
            <el-input
              v-model="queryParams.mtpName"
              placeholder="请输入资产名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用部门" prop="mtpDept">
            <el-input
              v-model="queryParams.mtpDept"
              placeholder="请输入使用部门"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存放地点" prop="mtpLocation">
            <el-input
              v-model="queryParams.mtpLocation"
              placeholder="请输入存放地点"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改固定资产信息对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="资产编号" prop="mtpOrder">
            <el-input v-model="form.mtpOrder" placeholder="请输入资产编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类别" prop="mtpEquipmentType">
            <el-input
              v-model="form.mtpEquipmentType"
              placeholder="请输入设备类别"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="资产名称" prop="mtpName">
            <el-input v-model="form.mtpName" placeholder="请输入资产名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格型号" prop="mtpModel">
            <el-input v-model="form.mtpModel" placeholder="请输入规格型号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备厂商" prop="mtpEquipmentFirm">
            <el-input
              v-model="form.mtpEquipmentFirm"
              placeholder="请输入设备厂商"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="购入日期" prop="mtpBuyDate">
            <el-date-picker
              clearable
              size="small"
              v-model="form.mtpBuyDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择购入日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单位" prop="mtpUnit">
            <el-select
              v-model="form.mtpUnit"
              filterable
              placeholder="单位"
              clearable
            >
              <el-option
                v-for="item in unitFlagOptions"
                :key="item.dictLabel"
                :label="item.dictLabel"
                :value="item.dictLabel"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="数量" prop="mtpNum">
            <el-input v-model="form.mtpNum" placeholder="数量" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-col :span="12">
            <el-form-item label="使用部门" prop="mtpDept">
              <el-select
                v-model="form.mtpDept"
                style="width: 220px"
                filterable
                placeholder="使用部门"
                clearable
              >
                <el-option
                  v-for="item in deptList"
                  :key="item.dictLabel"
                  :label="item.dictLabel"
                  :value="item.dictLabel"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任人" prop="mtpDutyName">
            <el-input v-model="form.mtpDutyName" placeholder="请输入责任人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存放地点" prop="mtpLocation">
            <el-input v-model="form.mtpLocation" placeholder="请输入存放地点" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="mtpDesc">
            <el-input v-model="form.mtpDesc" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 营销单导入对话框 -->
    <el-dialog
      :title="upload.title"
      v-model="upload.open"
      :close-on-click-modal="false"
      width="400px"
      draggable
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :data="{ somType: this.$route.query.somType }"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">取 消</el-button>
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
      </div>
    </el-dialog>
    <hd-print-page ref="printPage" width="115mm">
      <div class="content">
        <div v-for="item in selectionData">
          <el-row>
            <el-col :span="6" style="padding-top: 5mm"
              ><img src="@/assets/images/favorite.png" height="39px"
            /></el-col>
            <el-col :span="12" style="text-align: center">
              <div style="font-size: 16px">武汉衡德实业有限公司</div>
              <div style="font-size: 24px">固定资产标签</div>
            </el-col>
            <el-col :span="6" style="line-height: normal">
              <VueQr :text="item.mtpOrder" :size="88" :margin="5"></VueQr>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="printrow">
                <div>资产编号：</div>
                <div class="pline">{{ item.mtpOrder }}</div>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <div class="printrow">
                <div>资产名称：</div>
                <div class="pline">{{ item.mtpName }}</div>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="printrow">
                <div>使用部门：</div>
                <div class="pline">{{ item.mtpDept }}</div>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="printrow">
                <div>规格型号：</div>
                <div class="pline">{{ item.mtpModel }}</div>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="printrow">
                <div>盘点日期：</div>
                <div class="pline"></div>
              </div>
            </el-col>
          </el-row>
          <div style="text-align: center">
            请爱护企业资产，不要随意撕毁此标签
          </div>
        </div>
      </div>
    </hd-print-page>
  </div>
</template>

<script>
import {
  listMtpMstr,
  getMtpMstr,
  delMtpMstr,
  addMtpMstr,
  updateMtpMstr,
  exportMtpMstr,
  exportMtpExample,
} from "@/api/money/mtpMstr";
import { getToken } from "@/utils/auth";
import { exportSomExample, importSomMstr } from "@/api/marketing/som";
import { getDeptList } from "@/api/system/dept";
import VueQr from "vue-qr";
export default {
  name: "MtpMstr",
  components: {
    VueQr,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 固定资产信息表格数据
      mtpMstrList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeMtpJlrq: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        mtpOrder: null,
        mtpName: null,
        mtpDept: null,
        mtpLocation: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mtpName: [
          { required: true, message: "资产名称不能为空", trigger: "blur" },
        ],
        mtpDept: [
          { required: true, message: "使用部门不能为空", trigger: "blur" },
        ],
        mtpDutyName: [
          { required: true, message: "责任人不能为空", trigger: "blur" },
        ],
      },
      // 全部部门
      deptList: [],
      unitFlagOptions: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/money/mtpMstr/importMtp",
      },
      selectionData: [{}],
    };
  },
  created() {
    // 获取全部部门
    getDeptList().then((res) => {
      this.deptList = res.data;
      for (let item of this.deptList) {
        item.dictLabel = item.deptName;
        item.listClass = "default";
        item.dictValue = item.deptId + "";
      }
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询固定资产信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeMtpJlrq && "" != this.daterangeMtpJlrq) {
        this.queryParams.params["beginMtpJlrq"] = this.moment(
          this.daterangeMtpJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endMtpJlrq"] = this.moment(
          this.daterangeMtpJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listMtpMstr(this.queryParams).then((response) => {
        this.mtpMstrList = [];
        this.mtpMstrList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listMtpMstr(this.queryParams).then((response) => {
          this.mtpMstrList = this.mtpMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mtpId: null,
        mtpOrder: null,
        mtpEquipmentType: null,
        mtpName: null,
        mtpModel: null,
        mtpEquipmentFirm: null,
        mtpBuyDate: new Date(),
        mtpUnit: null,
        mtpNum: null,
        mtpDept: null,
        mtpDutyName: null,
        mtpLocation: null,
        mtpDesc: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.mtpMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeMtpJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectionData = selection;
      this.ids = selection.map((item) => item.mtpId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加固定资产信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mtpId = row.mtpId || this.ids;
      getMtpMstr(mtpId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改固定资产信息";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询固定资产信息";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.mtpId != null) {
            updateMtpMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMtpMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mtpIds = row.mtpId || this.ids;
      this.$confirm(
        '是否确认删除固定资产信息编号为"' + mtpIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delMtpMstr(mtpIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleImport() {
      this.upload.title = "固定资产信息导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      exportMtpExample().then((response) => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    beforeUpload(file) {
      //文件效验,判断文件格式和文件大小
      const extension = file.name.split(".")[1] === "xls";
      const extension2 = file.name.split(".")[1] === "xlsx";
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!extension && !extension2) {
        this.$message.error("上传模板只能是 xls、xlsx格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传模板大小不能超过 10MB!");
      }

      //上传图片
      let formData = new FormData();
      formData.append("file", file);
      importSomMstr(formData).then((response) => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有固定资产信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportMtpMstr(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    handlePrint() {
      this.$refs.printPage.print();
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  width: 115mm;
  font-family: "宋体";
  font-weight: 700;
  > div {
    page-break-inside: avoid;
    padding: 3mm 0 0 0;
    line-height: 9.8mm;
    height: 75mm;
    .printrow {
      display: flex;
      .pline {
        flex: 1 1 auto;
        border-bottom: 1px solid;
      }
    }
  }
}
</style>
